package com.example.peibei.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.RadioButton;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.peibei.R;

import java.util.List;

public class PaidanTimeAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private int mSelectedItem = -1;
    private Context mContext;

    public PaidanTimeAdapter(@Nullable List<String> data, Context mContext) {
        super(R.layout.item_radiobutton_time, data);
        this.mContext = mContext;
    }

    @SuppressLint({"ResourceAsColor", "UseCompatLoadingForColorStateLists"})
    @Override
    protected void convert(BaseViewHolder helper, String item) {
        RadioButton bt_week = helper.getView(R.id.bt_week);
        String substring = item.substring(11, 16);
        bt_week.setText(substring);
        bt_week.setChecked(helper.getLayoutPosition() == mSelectedItem);
        bt_week.setTag(helper.getLayoutPosition());

        helper.setBackgroundResource(R.id.bt_week, R.drawable.select_radiobutton);
        if(bt_week.isChecked()){
            bt_week.setTextColor(mContext.getResources().getColorStateList(R.color.white));
        }else{
            bt_week.setTextColor(mContext.getResources().getColorStateList(R.color.gray_home));
        }
        bt_week.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSelectedItem = (int) v.getTag();
                notifyDataSetChanged();
                if (mListener != null) {
                    mListener.onItemClick(item, helper.getLayoutPosition());
                }
            }
        });
    }

    public interface OnItemClickListener {
        void onItemClick(String data, int pos);
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }
}
